package edu.gyc.hiwhore.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gyc.hiwhore.dao.GirlDao;
import edu.gyc.hiwhore.model.Girl;
import edu.gyc.hiwhore.redis.RedisUtils;
import edu.gyc.hiwhore.service.LoverService;
import edu.gyc.hiwhore.service.OlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2021-08-03
 */
@Service
@Slf4j
@CacheConfig(cacheNames = "whore")
public class OlServiceImpl extends ServiceImpl<GirlDao, Girl> implements OlService {
    @Autowired
    RedisUtils redisUtils;
    public static final String CACHE_KEY_GIRL = "lover:";
    public static final String CACHE_KEY_GIRL_ID = "loverid";
    private Set<Integer> loverIdSet = new HashSet<>();

    //其中#p0是指的第一个参数，#p1是第二个参数，以此类推。
    @Override
//    @Cacheable(key = "'girl-id-'+#p0")
//    @Cacheable(value = "girl",key = "#id",condition = "#id>1")
    public Girl getById(Serializable id) {
        Object o = redisUtils.get(CACHE_KEY_GIRL + id);
        if (o != null) {
            return (Girl) o;
        }

       log.info("From db query girl :"+id);
        Girl girl = super.getById(id);
        redisUtils.set(CACHE_KEY_GIRL + id, girl);
        loverIdSet.add(girl.getId());
        redisUtils.set(CACHE_KEY_GIRL_ID, loverIdSet);
        return girl;
    }

//    @CacheEvict(key = "'girl-id-'+#girl.id",allEntries = true)
//    @CachePut(value = "girl",key = "#girl.id")
    public Girl update(Girl girl) {
        log.info("update girl id:"+girl.getId());
//         this.updateById(girl);
        //只更新非空字段
        LambdaUpdateWrapper<Girl> updateWrapper = Wrappers.<Girl>lambdaUpdate()
                .eq(Girl::getId,girl.getId())
                .set(StringUtils.isNotBlank(girl.getName()), Girl::getName, girl.getName())
                .set(StringUtils.isNotBlank(girl.getGender()),Girl::getGender,girl.getGender())
//                .set(ObjectUtils.isNotNull(girl.getBirthday()),Girl::getBirthday,girl.getBirthday())
                .set(StringUtils.isNotBlank(girl.getMajor()),Girl::getMajor,girl.getMajor())
                .set(ObjectUtils.isNotNull(girl.getPrice()),Girl::getPrice,girl.getPrice());


        this.update(updateWrapper);
//        Girl t = getById(girl.getId());
//        for(Girl )
        Girl girl1=super.getById(girl.getId());
        redisUtils.set(CACHE_KEY_GIRL + girl.getId(), girl1);

        return girl1;
    }

    @Override
//    @CacheEvict(key = "'girl-id-'+#p0")
    public boolean del(Integer id) {
        boolean r=this.removeById(id);
        redisUtils.del(CACHE_KEY_GIRL+id);
        Set<Integer> loverIdSet = (Set<Integer>) redisUtils.get(CACHE_KEY_GIRL_ID);
        loverIdSet.remove(id);
        redisUtils.set(CACHE_KEY_GIRL_ID, loverIdSet);
        return r;
    }

    @Override
//    @CachePut(key = "'girl-id-'+#girl.id")
    public boolean addGirl(Girl girl) {
        boolean r = this.save(girl);


        redisUtils.set(CACHE_KEY_GIRL + girl.getId(), girl);
        Set<Integer> loverIdSet = (Set<Integer>) redisUtils.get(CACHE_KEY_GIRL_ID);
        loverIdSet.add(girl.getId());
        redisUtils.set(CACHE_KEY_GIRL_ID, loverIdSet);
        return r;
    }

    @Override
    public  void  cacheAllGirl() {
       List<Girl> girls=this.list();
        for (Girl girl : girls) {
            redisUtils.set(CACHE_KEY_GIRL + girl.getId(), girl);
            loverIdSet.add(girl.getId());
        }
        redisUtils.set(CACHE_KEY_GIRL_ID, loverIdSet);
    }

    @Override
    public List<Girl> listGirls() {
        Set<Integer> ids=(Set<Integer>) redisUtils.get(CACHE_KEY_GIRL_ID);
        List<Girl> girls = new ArrayList<>();
        for (Integer id : ids) {
            girls.add((Girl) redisUtils.get(CACHE_KEY_GIRL + id));
        }
        return girls;
    }


}
